package com.zhanggm.springcloud.jpa.controller;


import com.zhanggm.springcloud.jpa.entity.StuStudent;
import com.zhanggm.springcloud.jpa.repository.StuClassesRepository;
import com.zhanggm.springcloud.jpa.repository.StuStudentRepository;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@RestController
public class DemoController {

    @Autowired
    private StuStudentRepository stuStudentRepository;

    @GetMapping("getPageInfo")
    public Object getPageInfo(StuStudent stuStudent, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
       /* ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withMatcher("username", ExampleMatcher.GenericPropertyMatchers.contains());

        Example example = Example.of(userEntity,exampleMatcher);
        Pageable pageable = PageRequest.of(pageNum-1,pageSize, Sort.Direction.DESC,"sex");
        Page all = userRepository.findAll(example, pageable);*/

        Specification<StuStudent> specification = new Specification<StuStudent>() {
            @Override
            public Predicate toPredicate(Root<StuStudent> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                //保存条件list
                List<Predicate> predicateList = new ArrayList<>();
                //构建查询条件
                if(stuStudent.getName()!=null){
                    Predicate name = criteriaBuilder.like(root.get("name"), "%".concat(stuStudent.getName()).concat("%"));
                    predicateList.add(name);
                }
                //根据爱好模糊查询学生
                if(stuStudent.getClassesName()!=null){
                    Predicate like = criteriaBuilder.like(root.get("stuClasses").get("name"), "%".concat(stuStudent.getClassesName()).concat("%"));
                    predicateList.add(like);
                }
                //根据爱好名称查询学生
                if(stuStudent.getHobbyName()!=null){
                    Predicate like = criteriaBuilder.like(root.join("hobbyList").get("name"), "%".concat(stuStudent.getHobbyName()).concat("%"));
                    predicateList.add(like);
                }
                //In条件
                if(stuStudent.getClassesIdList()!=null){
                    CriteriaBuilder.In<Object> classesId = criteriaBuilder.in(root.get("classesId"));
                    classesId.in(stuStudent.getClassesIdList());
                    predicateList.add(classesId);
                }
                //把条件转化成数组
              /*  Predicate[] predicates = new Predicate[predicateList.size()];
                predicateList.toArray(predicates);*/
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
//                return criteriaBuilder.and(predicates);
            }
        };
        Pageable pageable = PageRequest.of(pageNum-1,pageSize, Sort.Direction.DESC,"name");
        Page<StuStudent> all = stuStudentRepository.findAll(specification, pageable);


        return all;
    }

    @GetMapping("getAllStudent")
    public Object getAllStudent(){
        return stuStudentRepository.findAll();
    }

    @Autowired
    private StuClassesRepository stuClassesRepository;


    @GetMapping("getAllClasses")
    public Object getAllClasses(){
        return stuClassesRepository.findAll();
    }
}
